package com.controller;

import java.io.FileInputStream;
import java.io.IOException;

import jakarta.servlet.ServletException;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/down")
public class DownloadServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	//	resp.setContentType("applicaton/msword");
		resp.setContentType("application/pdf");
		//先在服务器上读取文件,然后写出去
		String path = req.getParameter("path");
		FileInputStream fis = new FileInputStream(path);
		ServletOutputStream sos = resp.getOutputStream();
		resp.setHeader("Content-disposition", "attatchment;filename=a.pdf");
		int i = 0;
		while(true) {
			i = fis.read();
			if(i != -1) {
				sos.write(i);
			}else {
				break;
			}
		}
		sos.close();
		fis.close();
	}
		
}
